package com.swust.monitorProject.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.swust.monitorProject.entity.Dept;
import com.swust.monitorProject.entity.User;
import com.swust.monitorProject.mapper.DeptMapper;
import com.swust.monitorProject.mapper.UserMapper;
import com.swust.monitorProject.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author shuaixin
 * @since 2024-12-24
 */
@Primary
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    private  UserMapper userMapper;
    @Autowired
    private  DeptMapper deptMapper;
    private  PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();

    @Override
    public Boolean updatePassword(Long id, String userPassword) {
        User user = new User();
        user.setUserId(id); // 假设User实体类中有一个userId属性
        user.setPassword(userPassword);
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("user_ID", id); // 假设数据库中用户user_ID的列名为user_ID
        return userMapper.update(user, updateWrapper) > 0;
    }
    /**
     * 创建新用户并更新对应部门的人数
     */
    @Override
    public Boolean createUser(User user) {
        // 插入新用户
        if (userMapper.insert(user) != 1) {
            return false; // 插入失败
        }
        // 更新对应部门的人数
        Dept dept = deptMapper.selectById(user.getUserDeptId()); // 根据dept_id查询部门
        if (dept != null) {
            dept.setDeptNum(dept.getDeptNum() + 1); // 增加部门人数
            return deptMapper.updateById(dept) > 0; // 更新部门人数
        }
        return false; // 部门不存在
    }
    @Override
    public Boolean deleteUser(Long id) {
        // 查询用户
        User user = userMapper.selectById(id);
        // 获取用户所属部门名称
        int deptNameID = user.getUserDeptId(); // 假设这个字段存储了部门名称
        // 删除用户
        int deleteCount = userMapper.deleteById(id);
        if (deleteCount > 0) {
            // 更新对应部门的人数
            Dept dept = deptMapper.selectOne(new QueryWrapper<Dept>().eq("dept_id", deptNameID));
            if (dept != null) {
                dept.setDeptNum(dept.getDeptNum() - 1); // 减少部门人数
                deptMapper.updateById(dept); // 更新部门人数
            }
            return true; // 删除成功
        }
        return false; // 删除失败
    }

    @Override
    public boolean changePsw(Long userId, String newPassword) {
        User user = userMapper.selectById(userId);
        if (user == null) {
            return false;
        }
        // 使用注入的 PasswordEncoder 加密新密码
        String encodedPassword = passwordEncoder.encode(newPassword);
        user.setPassword(encodedPassword);
        return userMapper.updateById(user) > 0;
    }

    @Override
    public List<User> getAllUsers() {
        return userMapper.selectList(null);
    }




}